package com.gxa.service.system.impl;


import com.gxa.mapper.AdminMapper;
import com.gxa.service.system.AdminService;
import com.gxa.vo.AdminVo;
import com.gxa.vo.MenuVo;
import com.gxa.vo.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;

    @Override
    public List<AdminVo> queryAllAdmin() {
        List<AdminVo> adminVos1 = adminMapper.queryAllAdmin();
        List<AdminVo> adminVos = new ArrayList<>();
        for(AdminVo adminVo:adminVos1){
            if(adminMapper.selRoleNameByUId(adminVo.getId())!=null) {
                RoleVo roleVo = adminMapper.selRoleNameByUId(adminVo.getId());
                System.out.println(roleVo);
                adminVo.setRoleName(roleVo.getName());
            }
            adminVos.add(adminVo);
        }
        System.out.println("service:"+adminVos);
        return adminVos;
    }

    @Override
    public List<AdminVo> queryAdminsByName(String name) {
        List<AdminVo> adminVos = adminMapper.qureyAdminsByName(name);
        return adminVos;
    }

    @Override
    public List<RoleVo> queryAllRole() {
        List<RoleVo> roleVos = adminMapper.queryAllRole();
        return roleVos;
    }

    @Override
    public void addAdmin(AdminVo adminVo) {
        Integer r_id = adminMapper.queryRidByName(adminVo.getRoleName());
        System.out.println(r_id);
        Integer a_id = adminMapper.addAdmin(adminVo);
        System.out.println(a_id);
        adminMapper.addAdminRole(a_id,r_id);
    }

    @Override
    public AdminVo queryAdminById(Integer id) {
        AdminVo adminVo = adminMapper.queryAdminById(id);
        return adminVo;
    }

    @Override
    public void updateAdmin(AdminVo adminVo) {
        Integer r_id = adminMapper.queryRidByName(adminVo.getRoleName());
        adminMapper.updateUserRole(adminVo.getId(),r_id);
        adminMapper.updateAdmin(adminVo);
    }


    @Override
    public boolean delAdmin(Integer id) {
        if (adminMapper.delAdmin(id)!=0){
            return true;
        }
        return false;
    }

    @Override
    public void updateAdminStatus(Integer id, Integer status) {
        adminMapper.updateAdminStatus(id,status);
    }

    @Override
    public void updateAdminPwd(Integer id, String pwd) {
        adminMapper.updateAdminPwd(id,pwd);
    }

    @Override
    public Integer queryAdmin(AdminVo adminVo) {
        AdminVo adminVo1 = adminMapper.queryAdmin(adminVo);
        if(adminVo1!=null){
            return adminVo1.getId();
        }else {
            return null;
        }
    }

    @Override
    public List<MenuVo> queryMenus(Integer user_id) {
        List<MenuVo> menuVos = adminMapper.queryMenus(user_id);
        return menuVos;
    }

    @Override
    public Integer addUser(AdminVo adminVo) {
        Integer integer = adminMapper.addAdmin(adminVo);
        Integer integer1 = adminMapper.addPatient(adminVo.getName(), adminVo.getIdCard(), adminVo.getIdCard());
        adminMapper.addUserRole(adminVo.getId());
        return integer+integer1;
    }

    @Override
    public Set<String> queryMenusByName(String name) {
        Set<String> strings = adminMapper.queryMenusByName(name);
        return strings;
    }

    @Override
    public AdminVo queryAdminByAccount(String account) {
        AdminVo adminVo = adminMapper.queryAdminByAccount(account);
        return adminVo;
    }

    @Override
    public List<MenuVo> selPMenu(Integer user_id) {
        List<MenuVo> menuVos = adminMapper.selPMenu(user_id);
        return menuVos;
    }

    @Override
    public List<MenuVo> selSMenu(Integer id) {
        List<MenuVo> menuVos = adminMapper.selSMenu(id);
        return menuVos;
    }
}
